const ethers = require("ethers")
const fs = require("fs-extra")
require("dotenv").config()

async function main() {
    // 编译合约代码
    // yarn solcjs --bin --abi --include-path node_modules/ --base-path . -o . SimpleStorage.sol
    // rpc interface: https://playground.open-rpc.org/?schemaUrl=https://raw.githubusercontent.com/ethereum/execution-apis/assembled-spec/openrpc.json&uiSchema%5BappBar%5D%5Bui:splitView%5D=false&uiSchema%5BappBar%5D%5Bui:input%5D=false&uiSchema%5BappBar%5D%5Bui:examplesDropdown%5D=false
    // http://127.0.0.1:7545

    // 本地区块链的部署地址
    const provider = new ethers.JsonRpcProvider(process.env.RPC_URL)

    // 获取一个钱包地址
    const wallet = new ethers.Wallet(process.env.PRIVATE_KEY, provider)
    // 获取加密的数据
    // const encryptedJson = fs.readFileSync("./encryptedKey.json", "utf8")
    // // 此处不需要使用 new
    // let wallet = ethers.Wallet.fromEncryptedJsonSync(
    //     encryptedJson,
    //     process.env.PRIVATE_KEY_PASSWORD,
    // )

    // wallet = await wallet.connect(provider)

    // 我们编译之后的 abi
    const abi = fs.readFileSync("./SimpleStorage_sol_SimpleStorage.abi", "utf8")

    // 我们编译之后的 二进制文件
    const binary = fs.readFileSync(
        "./SimpleStorage_sol_SimpleStorage.bin",
        "utf8",
    )

    // 创建一个合约工厂
    const contractFactory = new ethers.ContractFactory(abi, binary, wallet)
    console.log("Deploying, Please wait...")
    // 进行合约的部署
    const contract = await contractFactory.deploy({})
    // 交易回执等待多少个区块确认之后，返回
    const deploymentReceipt = await contract.deploymentTransaction().wait(1)
    console.log(`Contract Address: ${await contract.getAddress()}`)

    // console.log("Let's deploy with only transaction data!");
    // const nonce = await wallet.getNonce();
    // const tx = {
    //   nonce: nonce, // 使用nonce来尝试解决挖矿难题；也被用于在钱包中给交易签名
    //   gasPrice: 20000000000, //
    //   gasLimit: 1000000,
    //   to: null,
    //   value: 0,
    //   data: "0x608060405234801561001057600080fd5b506108a6806100206000396000f3fe608060405234801561001057600080fd5b50600436106100935760003560e01c80636932e6a5116100665780636932e6a5146101215780636bc646f7146101515780636f760f41146101815780638bd95dd71461019d578063fccdb748146101cd57610093565b80631f1094e7146100985780632e64cec1146100c95780634f2be91f146100e75780636057361d14610105575b600080fd5b6100b260048036038101906100ad91906105a2565b6101eb565b6040516100c092919061067a565b60405180910390f35b6100d16102a7565b6040516100de919061065f565b60405180910390f35b6100ef6102b0565b6040516100fc919061065f565b60405180910390f35b61011f600480360381019061011a91906105a2565b6102b9565b005b61013b600480360381019061013691906105a2565b6102cc565b604051610148919061065f565b60405180910390f35b61016b600480360381019061016691906105a2565b6102e7565b604051610178919061065f565b60405180910390f35b61019b60048036038101906101969190610546565b61030b565b005b6101b760048036038101906101b291906104fd565b6103a1565b6040516101c4919061065f565b60405180910390f35b6101d56103cf565b6040516101e2919061065f565b60405180910390f35b600281815481106101fb57600080fd5b906000526020600020906002020160009150905080600001549080600101805461022490610773565b80601f016020809104026020016040519081016040528092919081815260200182805461025090610773565b801561029d5780601f106102725761010080835404028352916020019161029d565b820191906000526020600020905b81548152906001019060200180831161028057829003601f168201915b5050505050905082565b60008054905090565b60006002905090565b806000819055506102c86102a7565b5050565b600481600381106102dc57600080fd5b016000915090505481565b600381815481106102f757600080fd5b906000526020600020016000915090505481565b6000604051806040016040528083815260200184815250905060028190806001815401808255809150506001900390600052602060002090600202016000909190919091506000820151816000015560208201518160010190805190602001906103769291906103d5565b5050508160078460405161038a9190610648565b908152602001604051809103902081905550505050565b6007818051602081018201805184825260208301602085012081835280955050505050506000915090505481565b60015481565b8280546103e190610773565b90600052602060002090601f016020900481019282610403576000855561044a565b82601f1061041c57805160ff191683800117855561044a565b8280016001018555821561044a579182015b8281111561044957825182559160200191906001019061042e565b5b509050610457919061045b565b5090565b5b8082111561047457600081600090555060010161045c565b5090565b600061048b610486846106cf565b6106aa565b9050828152602081018484840111156104a7576104a6610839565b5b6104b2848285610731565b509392505050565b600082601f8301126104cf576104ce610834565b5b81356104df848260208601610478565b91505092915050565b6000813590506104f781610859565b92915050565b60006020828403121561051357610512610843565b5b600082013567ffffffffffffffff8111156105315761053061083e565b5b61053d848285016104ba565b91505092915050565b6000806040838503121561055d5761055c610843565b5b600083013567ffffffffffffffff81111561057b5761057a61083e565b5b610587858286016104ba565b9250506020610598858286016104e8565b9150509250929050565b6000602082840312156105b8576105b7610843565b5b60006105c6848285016104e8565b91505092915050565b60006105da82610700565b6105e4818561070b565b93506105f4818560208601610740565b6105fd81610848565b840191505092915050565b600061061382610700565b61061d818561071c565b935061062d818560208601610740565b80840191505092915050565b61064281610727565b82525050565b60006106548284610608565b915081905092915050565b60006020820190506106746000830184610639565b92915050565b600060408201905061068f6000830185610639565b81810360208301526106a181846105cf565b90509392505050565b60006106b46106c5565b90506106c082826107a5565b919050565b6000604051905090565b600067ffffffffffffffff8211156106ea576106e9610805565b5b6106f382610848565b9050602081019050919050565b600081519050919050565b600082825260208201905092915050565b600081905092915050565b6000819050919050565b82818337600083830152505050565b60005b8381101561075e578082015181840152602081019050610743565b8381111561076d576000848401525b50505050565b6000600282049050600182168061078b57607f821691505b6020821081141561079f5761079e6107d6565b5b50919050565b6107ae82610848565b810181811067ffffffffffffffff821117156107cd576107cc610805565b5b80604052505050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052602260045260246000fd5b7f4e487b7100000000000000000000000000000000000000000000000000000000600052604160045260246000fd5b600080fd5b600080fd5b600080fd5b600080fd5b6000601f19601f8301169050919050565b61086281610727565b811461086d57600080fd5b5056fea26469706673582212203cba4bc1ed75f5e9e22505197340749fd8ccc6efc1246a58ec5c77a78f017c0064736f6c63430008070033",
    //   chainId: 1337, // error transaction chainId mismatch
    // };

    // // 对交易签名，但是没有发送
    // // const signedTxResponse = await wallet.signTransaction(tx);
    // const sendTxResponse = await wallet.sendTransaction(tx); // 在sendTransaction内部，会调用signTransaction
    // await sendTxResponse.wait(1);
    // console.log("sendTxResponse:" + sendTxResponse);

    // 这个合约对象 contract 将拥有我们合约中的 abi 的功能
    let currentFavoriteNumber = await contract.retrieve()
    console.log(currentFavoriteNumber)
    // 当然，这里也可以传入 string 类型的数据，因为如果是一个很大的数据，js 语法是不支持的
    // 这里一样需要
    const storeResp = await contract.store(1)
    // 必须拿到函数返回的结果，再 await
    await storeResp.wait(1)
    console.log((await contract.retrieve()).toString())
    console.log(
        `new currentFavoriteNumber is: ${(await contract.retrieve()).toString()}`,
    )
}

// 运行 main 函数
main()
    .then(() => process.exit(0))
    .catch((error) => {
        console.log(error)
        process.exit(1)
    })
